package Raid;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.geom.*;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.geom.ShapeRenderer;


public abstract class Baddy {

	double x;
	double y;
	double width;
	double height;
	
	double life;
	double maxLife;
	
	double xSpeed;
	double maxXSpeed;
	double ySpeed;
	double maxYSpeed;
	double gravity;
	
	int facing;
	
	BaddyType type;
	Texture texture;
	
	double maskWidth;
	double maskHeight;
	Rectangle mask;
	
	boolean standing;
	boolean jumping;
	
	int jumpSpacing;
	int timeSinceJump;
	int maxJumps;
	int jumps;
	
	int texID;
	
	Environment env;
	
	abstract void render();
	abstract void doLogic(int delta);
	abstract boolean takeDamage(double damage);
	
	public void updatePhysics(int delta){
		fallingPhysics(delta);
		horizontalPhysics(delta);
		jumpLogic(delta);
		if (ySpeed > maxYSpeed){
			ySpeed = maxYSpeed;
		}
		if (xSpeed > maxXSpeed){
			xSpeed = maxXSpeed;
		} else if (xSpeed < -maxXSpeed){
			xSpeed = -maxXSpeed;
		}
		x += xSpeed*delta;
		y += ySpeed*delta;
		if (x > Raid.windowWidth){
			x = Raid.windowWidth;
		} else if (x < 0){
			x = 0;
		}
		if (y > Raid.windowHeight){
			y = Raid.windowHeight;
		} else if (y < 0){
			y = 0;
		}	
	}
	
	public void fallingPhysics(int delta){
		ySpeed += gravity*delta;
		standing = false;
		Rectangle newMask = getMask(0, (float)ySpeed);
		if (ySpeed > 0){
			Block[] beneathBlocks = env.getBeneathBlocks(x, y, width);
			for (int i = 0; i < 3; i++){
				if (beneathBlocks[i] != null && beneathBlocks[i].solid && beneathBlocks[i].mask.getMinY() - newMask.getMinY() > 8 
						&& Utils.intersects(beneathBlocks[i].mask, newMask)){
					ySpeed = 0;
					y = beneathBlocks[i].y - width;
					standing = true;
					jumps = 0;
					break;
				}
			}
		} else if (ySpeed < 0){
			Block[] aboveBlocks = env.getAboveBlocks(x, y, width);
			for (int i = 0; i < 6; i++){
				if (aboveBlocks[i] != null && aboveBlocks[i].solid 
						&& Utils.intersects(aboveBlocks[i].mask, newMask)){
					ySpeed = 0;
					y = aboveBlocks[i].y + Block.width;
					break;
				}
			}
		}
	}
	
	
	public void jumpLogic(int delta){
		if (jumping == true){
			if (timeSinceJump >= jumpSpacing && jumps < maxJumps){
				ySpeed = -maxYSpeed;
				timeSinceJump = 0;
				jumps += 1;
			}
		}
		timeSinceJump += delta;
		if (timeSinceJump > 50000){
			timeSinceJump = jumpSpacing+1;
		}
	}
	
	
	public void horizontalPhysics(int delta){
		Block[] sideBlocks = env.getSideBlocks(x, y, width);
		Rectangle newMask = getMask((float)xSpeed, 0f);
		if (xSpeed > 0){
			for (int i = 1; i < 6; i+=2){
				if (sideBlocks[i] != null && sideBlocks[i].solid && Utils.intersects(sideBlocks[i].mask, newMask)){
					xSpeed = 0;
					x = sideBlocks[i].x - mask.getWidth()/2.0f;
					break;
				}
			}
		} else if (xSpeed < 0) {
			for (int i = 0; i < 6; i+=2){
				if (sideBlocks[i] != null && sideBlocks[i].solid && Utils.intersects(sideBlocks[i].mask, newMask)){
					xSpeed = 0;
					x = sideBlocks[i].x + Block.width + mask.getWidth()/2.0f;
					break;
				}
			}
		}
	}
	
	
	public Rectangle getMask(float xs, float ys){
		return new Rectangle((float)(x-maskWidth/2) + xs, (float)y + ys, (float)(maskWidth), (float)maskHeight);
	}
	
	
}
